/**
 * Created by ASUS on 2017/12/21.
 */
//购物车页面基础功能
function initShopCar(obj){
    var defaultObj = {
        carData:null,
        vueObj:null,
        nextPage:null
    };
    for(var i in obj){
        defaultObj[i] = obj[i]
    }

    if(defaultObj.vueObj==null){alert("需要指定vue作用域的ID");return;}
    var carData = defaultObj.carData;

    // 默认获取购物车缓存数据,如果用户登录.获取用户登录后的购物车数据
    if(carData==null){
    // 如果数据为空,需要自行获取
        carData = {
            isedit:false,
            totalPrice:0,
            totalSelect:0,
            totalDiscount:0,
            isSelect:false,
            arr:[
                {
                    id:"1",
                    hospitalName:"仁爱医院",
                    isSelect:false,
                    isedit:false,
                    hospitalProduct:[
                        {
                            id:"11",
                            isSelect:false,
                            productName:"豪华型体检套餐",
                            nowPrice:"1000",
                            oldPrice:"2000",
                            count:2
                        },
                        {
                            id:"12",
                            isSelect:false,
                            productName:"简单型体检套餐",
                            nowPrice:"1000",
                            oldPrice:"2000",
                            count:2
                        }
                    ]
                },
                {
                    id:"2",
                    hospitalName:"星耀医院",
                    isSelect:false,
                    isedit:false,
                    hospitalProduct:[
                        {
                            id:"21",
                            isSelect:false,
                            productName:"男性体检套餐",
                            nowPrice:"1000",
                            oldPrice:"2000",
                            count:2
                        },
                        {
                            id:"22",
                            isSelect:false,
                            productName:"女性体检套餐",
                            nowPrice:"1000",
                            oldPrice:"2000",
                            count:2
                        }
                    ]
                },
                {
                    id:"3",
                    hospitalName:"星耀医院",
                    isSelect:false,
                    isedit:false,
                    hospitalProduct:[
                        {
                            id:"21",
                            isSelect:false,
                            productName:"男性体检套餐",
                            nowPrice:"1000",
                            oldPrice:"2000",
                            count:2
                        },
                        {
                            id:"22",
                            isSelect:false,
                            productName:"女性体检套餐",
                            nowPrice:"1000",
                            oldPrice:"2000",
                            count:2
                        }
                    ]
                }
            ]
        }


    }else{

    }

//初始化vue作用域
    new Vue({
        el: defaultObj.vueObj,
        data:carData,
        methods:{
            move:function(hospital,product){
                hospital.hospitalProduct.map(function(ele,index){
                    if(ele.id == product.id){
                        hospital.hospitalProduct.remove(ele);
                    }
                });
                if(hospital.hospitalProduct.length == 0){
                    this.arr.remove(hospital);
                }
            },
            selectAll:function(){//点击页脚全选时
                this.isSelect = !this.isSelect;
                var _this = this;
                //控制医院以及医院套餐的显示和页脚信息展示状态
                if(this.isSelect){
                    this.arr.map(function(ele){
                       ele.isSelect = true;
                       ele.hospitalProduct.map(function(ele2){
                           if(ele2.isSelect&&_this.isSelect){
                               return true;
                           }
                           _this.countFooter(ele2,_this.isSelect);
                           ele2.isSelect = true;
                       })
                    });
                }else{
                    this.arr.map(function(ele){
                        ele.isSelect = false;
                        ele.hospitalProduct.map(function(ele2){
                            ele2.isSelect = false;
                            _this.countFooter(ele2,_this.isSelect);
                        })
                    });
                }


            },
            selectHospital:function(hospital){//点击选择医院时
                hospital.isSelect = !hospital.isSelect;
                var _this = this;
                //选择医院时需要判断全选按钮的显示逻辑
                if(hospital.isSelect){
                    var flag = true;
                    _this.arr.map(function(ele){
                        if(ele.isSelect){
                            return true;
                        }
                        flag = false;
                    });
                    if(flag){
                        _this.isSelect = true;
                    }
                }else{
                    _this.isSelect = false;
                }
                //控制页脚信息展示
                hospital.hospitalProduct.map(function(ele){//循环当前医院下的套餐
                    //如果选中医院时当前套餐已经是选中状态,则需要忽略本次循环
                    if(ele.isSelect&&hospital.isSelect){
                        return true;
                    }
                    _this.countFooter(ele,hospital.isSelect);//计算当前页面页脚信息的值
                    ele.isSelect = hospital.isSelect;
                });
            },
            select:function(product,hospital){//点击单个套餐按钮的选择时
                product.isSelect = !product.isSelect;
                if(!product.isSelect){//非选择状态.取消医院和全选的选择状态
                    hospital.isSelect = false;
                    this.isSelect = false;
                }else{//选择状态,需要判断当前已选数量是否等于医院套餐数量,以及医院置为选择状态后的医院数量是否满足全选状态的医院数量
                    var flag = true;
                    hospital.hospitalProduct.map(function(ele){
                        if(ele.isSelect){
                            return true;
                        }
                        flag = false;
                    });
                    if(flag){
                        hospital.isSelect = true;
                        var flag2 = true;
                        this.arr.map(function(ele){
                            if(ele.isSelect){
                                return true;
                            }
                            flag2 = false;
                        });
                        if(flag2){
                            this.isSelect = true;
                        }else{
                            this.isSelect = false;
                        }
                    }
                }
                //根据当前选择状态控制页脚信息展示
                this.countFooter(product,product.isSelect);//计算当前页面页脚信息的值
            },
            getDisCount:function(){

            },
            editAll:function(){
                this.isedit = !this.isedit;
                var _this = this;
                if(this.isedit){
                    this.arr.map(function(ele){
                        ele.isedit = true;
                    })
                }else{
                    this.arr.map(function(ele){
                        ele.isedit = false;
                    })
                }
            },
            editHospital:function(hospital,editFlag){
                if(editFlag){//当前是编辑状态中
                    hospital.isedit = false;
                    var flag = false;
                    this.arr.map(function(ele){
                        if(ele.isedit){
                            flag = true;
                            return false;
                        }
                    });
                    if(flag){
                        this.isedit = true;
                    }else{
                        this.isedit = false;
                    }
                }else{//当前是非编辑状态
                    hospital.isedit = true;
                    this.isedit = true;
                }
            },
            countControl:function(countflag,product){
                var disCount = Number(product.oldPrice)-Number(product.nowPrice);//计算当前套餐的已优惠价格
                if(countflag){
                    product.count = product.count+1;
                    if(product.isSelect) {
                        this.totalSelect = this.totalSelect + 1;
                        this.totalPrice = this.totalPrice + Number(product.nowPrice);
                        this.totalDiscount = this.totalDiscount + disCount;
                    }

                }else{
                    if(product.count<=1){

                    }else{
                        product.count = product.count-1;
                        if(product.isSelect) {
                            this.totalSelect = this.totalSelect - 1;
                            this.totalPrice = this.totalPrice - Number(product.nowPrice);
                            this.totalDiscount = this.totalDiscount - disCount;
                        }
                    }
                }
                if(product.isSelect){
                    var disCount = Number(product.oldPrice)-Number(product.nowPrice);//计算当前套餐的已优惠价格

                }
            },
            countFooter:function(ele,isSelect){ // 计算当前页面页脚信息的函数
                var disCount = Number(ele.oldPrice)-Number(ele.nowPrice);//计算当前套餐的已优惠价格
                if(isSelect){
                    this.totalSelect = this.totalSelect+Number(ele.count);//更改底部总选择数量
                    this.totalPrice = this.totalPrice + Number(ele.nowPrice)*Number(ele.count);//更改底部总价信息
                    this.totalDiscount = this.totalDiscount + disCount*Number(ele.count);//更改底部总优惠价格
                }else{
                    this.totalSelect = this.totalSelect-Number(ele.count);//更改底部总选择数量
                    this.totalPrice = this.totalPrice - Number(ele.nowPrice)*Number(ele.count);//更改底部总价信息
                    this.totalDiscount = this.totalDiscount - disCount*Number(ele.count);//更改底部总优惠价格
                }
            },
            goOrderCheck:function(){
                if(this.totalSelect == 0){return;}
                mui.openWindow({
                        url:defaultObj.nextPage
                });
            }
        }
    });
}

